import dayjs from 'dayjs'
import path from 'path'
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import packageJson from './package.json'
import zipPack from "vite-plugin-zip-pack"
import { fileURLToPath, URL } from 'node:url'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
const packageName = 'BDHSJPJ'
const currentDateAndTime = dayjs().format('YYYYMMDD')
const version = packageJson.version
const _targetPackName = `${packageName}-${currentDateAndTime}-V${version}`
export default defineConfig({
  // 其他配置...  
  build: {
    outDir: _targetPackName,
    rollupOptions: {
      manualChunks: {
        vueall: ["element-plus"]
      },
      // entry部分的文件命名，我们这里是spa,所以entry写死也没事,多入口的话需要注意了
      entryFileNames: "assets/js/[name]-[hash].js",
      // 自定义chunk如何命名法，包含懒加载或者自定义分包的一些内容的命名
      chunkFileNames: "assets/js/[name]-[hash].js",
      // 除js之外其他资源的存放
      assetFileNames: "assets/[ext]/[name]-[hash].[ext]",
      // compact用于压缩rollup产生的临时代码
      compact: true,
      // 设置为true之后,后续打包的时候只对更改的模块重新分析,加快打包速度
      cache: true
    }
  },
  plugins: [
    vue(),
    AutoImport({
      resolvers: [ElementPlusResolver()],
    }),
    Components({
      resolvers: [ElementPlusResolver()],
    }),
    zipPack({
      inDir: _targetPackName,
      outDir: 'archiver',
      outFileName: `${_targetPackName}.zip`,
      pathPrefix: _targetPackName
    })
  ],
  resolve: {
    alias: {
      // 设置路径
      '~': path.resolve(__dirname, './'),
      // 设置别名
      '@': fileURLToPath(new URL('./src', import.meta.url))
    },
    extensions: ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json', '.vue']
  },
  css: {
    postcss: {
      plugins: [
        {
          postcssPlugin: 'internal:charset-removal',
          AtRule: {
            charset: (atRule) => {
              if (atRule.name === 'charset') {
                atRule.remove();
              }
            }
          }
        }
      ]
    }
  },
  server: {
    //用来配置跨域
    host: '0.0.0.0',
    port: 621,
    proxy: {
      '/HeBeiHaiYangSJPJ': { // 需要代理的接口
        target: 'http://lc.guo.lol:28080', // 目标服务器
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/HeBeiHaiYangSJPJ/, '/HeBeiHaiYangSJPJ'),
      }
    }
  }
})
